package ca.kooki.codesnippet;

import ca.kooki.codesnippet.core.Identifier;

public class CodeSnippet {

  //```````````````````````
  // MEMBER VARIABLES
  //```````````````````````
  
  private Description _description;
  private String _code;
  private Identifier _id;
  
  //```````````````````````
  // CONSTRUCTOR
  //```````````````````````

  public CodeSnippet(String title, String keywords, String code)
  {
    _description = new Description(title,keywords);
    _code = code;
    _id = new Identifier(this.getClass());
  }
 
  
  //```````````````````````
  // INTERFACE
  //```````````````````````
  
  public boolean isOfficial()
  {
    return _id.isOfficial();
  }
  
  public String getId()
  {
    return _id.getId();
  }
  
  public  void setId(String newId)
  {
    _id.setId(newId);
  }
  
  public String getTitle() 
  {
    return _description.getTitle();
  }

  public String getKeywords() 
  {
    return _description.getKeywords();
  }

  public String getCode() 
  {
    return _code;
  }
  
  public String[] getKeywordList()
  {
    return _description.getKeywordList();
  }
  
  public boolean isSameCodeAs(CodeSnippet compareTo)
  {
    return getCode().trim().equals(compareTo.getCode().trim());
  }
  
  public String toString()
  {
    return String.format("%s%s", _description.toString(),_code);
  }
  
  public boolean equals(Object obj)
  {
    if (obj == null || !obj.getClass().equals(CodeSnippet.class))
    {
      return false;
    }

    CodeSnippet compareTo = (CodeSnippet)obj;
    
    if (compareTo.isOfficial() && isOfficial())
    {
      return compareTo.getId().equals(getId());
    }
    else
    {
      return this.toString().equals(compareTo.toString());
    }
  }

  
}
